<?php
/**
 * 在职和离职报表
*/
apf_require_class("Bll_BLL");
apf_require_class("Const_Report");
apf_require_class("Bll_Hr_OutInterface");
class Bll_Report_Report extends BLL{

    public function check_view_auth($uid){
        $count = $this->get_user_role()->find_count(array('user_id'=>$uid));
        return $count;
    }

    public function set_department_ids($ids){
        $this->department_ids = $ids;
    }
    /**
     * 得到当前在职用户 /report/
     * @param int $uid
     * @param int $all 读取所有部门,不计部门权限
     */
    public function get_current_on_job($uid,$all=1){
        if(empty($this->department_ids)){
            $this->department_ids = $this->get_role_dep_ids($uid);
        }
        if($all){
            $_where['department_id IN('.implode(',',$this->department_ids).') AND 1=?']=1;
        }
        $end = date('Ymd');
        $_where["enter_date<={$end} AND 1=?"]=1;
        $_where['job_status']=1;
        $datalist = $this->get_dep_user_dao()->find($_where,'',0);
        $data = array(
                'max'=>count($datalist),
                'p_num'=>0,
                'm_num'=>0,
                'plan_num'=>0
                );
        if(!empty($datalist)){
            $jobids=array();
            foreach($datalist as $val){
                if($val['p_level'] > 0){
                    $data['p_num'] +=1;
                }
                if($val['m_level'] > 0){
                    $data['m_num'] +=1;
                }
            }
            $hroa = new Bll_Hr_OutInterface();
            $job_list = $hroa->get_job_list();
            foreach($job_list as $val){
                if($all){
                    if(in_array($val['department_id'],$this->department_ids)){
                        $data['plan_num'] +=$val['plan_num'];
                    }
                }else{
                    $data['plan_num'] +=$val['plan_num'];
                }
            }
        }
        return $data;
    }
    private $_current_month;
    /**
     * 外部可以设置指定的时间后进行查看
     * @param unknown_type $month
     */
    public function set_current_month($month){
        $this->_current_month = strtotime($month);
    }
    /**
     * 取离职是从01-31号
     * @param unknown_type $uid
     * @param unknown_type $type
     * @return number
     */
    public function get_current_dimiss($uid,$type){
        if(empty($this->department_ids)){
            $this->department_ids = $this->get_role_dep_ids($uid);
        }
        //查询当前季度离职
        if(empty($this->_current_month)){
            $current_month = time();
        }else{
            $current_month = $this->_current_month;
        }
        $year = date('Y',$current_month);
        $data = array(
                'begin_num'=>0,
                'active_num'=>0,
                'passive_num'=>0,
                'dimiss_num'=>0
                );
        //先找出期初人数
        if($type=='season'){
            $month = date('n',$current_month);
            $s = $month/3;
            $m = $s<1 ? 1 : ceil($s);
            $mm = ($m-1)*3+1;

            for($i=0;$i<3;$i++){
                $reason_month[] = date('Ym',mktime(0,0,0,$mm+$i,1,$year));
            }
            $begin_m = date('Ym',mktime(0,0,0,$mm,0,$year));
            $start_m = date('Ym01',mktime(0,0,0,$mm,1,$year));
            $end_m = date('Ymd',mktime(0,0,0,$mm+3,0,$year));
        }else{
            $m = date('m',$current_month);
            $begin_m = date('Ym',mktime(0,0,0,$m,0,$year));
            $start_m = date('Ym01',$current_month);
            $end_m = date('Ymd',mktime(0,0,0,$m+1,0,$year));
        }
        $m_where['dateline'] = $begin_m;
        $m_where['department_id IN('.implode(',',$this->department_ids).') AND 1=?']=1;
        $month_data = $this->get_month_dao()->find($m_where);
        if(!empty($month_data)){
            foreach($month_data as $val){
                $dep_n = $val['begin_num']+$val['enter_num']-$val['active_num']-$val['passive_num'];
                $data['begin_num'] +=$dep_n;
            }
        }
        //分别查询入职人数和离职人数
        $_where['department_id IN('.implode(',',$this->department_ids).') AND 1=?']=1;
        $_where['enter_date >= '.$start_m.' AND enter_date <='.$end_m.' AND 1=?']=1;
        $_where['leaving_type !=3 AND 1=?'] = 1;
        $enter_num = $this->get_dep_user_dao()->find_count($_where);

        $_where2['department_id IN('.implode(',',$this->department_ids).') AND 1=?']=1;
        $_where2['last_date >= '.$start_m.' AND last_date <='.$end_m.' AND 1=?']=1;
        $_where2['leaving_type IN(1,2) AND 1=?']=1;
        $_where2['user_type IN(1,2) AND 1=?']=1;

        $dimiss_list = $this->get_dep_user_dao()->find($_where2,'',0);
        if(empty($dimiss_list)){
            $data['active_num']=$data['passive_num']=0;
        }else{
            foreach ($dimiss_list as $val){
                if($val['leaving_type']==1){
                    $data['active_num'] +=1;
                }elseif($val['leaving_type']==2){
                    $data['passive_num'] +=1;
                }
            }
        }
        $data['dimiss_num'] = $data['active_num']+ $data['passive_num'];
        $all_num = $data['begin_num']+$enter_num;
        $data['dimiss_rate'] = $this->_count_rate($data['dimiss_num'],$all_num);
        return $data;
    }
    /**
     * 当前月往前6个月的在职人数图表
     *
     */
    public function get_enter_report($uid){
        if(empty($this->department_ids)){
            $this->department_ids = $this->get_role_dep_ids($uid);
        }
        $_where['department_id IN('.implode(',',$this->department_ids).') AND 1=?']=1;
        for($i=1;$i<=6;$i++){
             $t = mktime(0,0,0,date('m')+1-$i,0,date('Y'));
             $month[] = date('Ym',$t);
        }
        $_where['dateline IN('.implode(',',$month).') AND 1=?']=1;
        $datalist = $this->get_month_dao()->find($_where,'dateline ASC',0);

        $data = array();
        $i=0;
        foreach($datalist as $val){
            $yearm = date('Y-m',strtotime($val['dateline'].'01'));
            $data[$val['dateline']]['on_job_num'] += $val['begin_num']+$val['enter_num']-$val['active_num']-$val['passive_num'];
            $data[$val['dateline']]['on_p_num'] +=$val['on_p_num'];
            $data[$val['dateline']]['on_m_num'] +=$val['on_m_num'];
            $data[$val['dateline']]['time']=$yearm;
            $i++;
        }
        //index
        $data = array_slice($data, 0,6);
        return $data;
    }
    private function _current_quarter(){
        $month = date('n');
        $s = $month/3;
        $m = $s<1 ? 1 : ceil($s);
        $mm = ($m-1)*3+1;
        return mktime(0,0,0,$mm,1,date('Y'));
    }
    /**
     * 前6季度的离职情况,前6个月的数据
     * @param int $uid
     */
    public function get_dimission_report($uid,$type='season'){
        if(empty($this->department_ids)){
            $this->department_ids = $this->get_role_dep_ids($uid);
        }

        if($type=='month'){
            $this->_current_month = mktime(0,0,0,date('m'),1,date('Y'));
        }else{
            $q_unix= $this->_current_quarter();
            $this->_current_month = mktime(0,0,0,date('m',$q_unix),1,date('Y',$q_unix));
        }

        $pre_m = array();
        for($i=1;$i<=18;$i++){
            $cm = date('m',$this->_current_month);
            $t = mktime(0,0,0,$cm+1-$i,0,date('Y',$this->_current_month));
            $month[$i] = date('Ym',$t);
            $m = date('n',$t);
            if(in_array($m,array(1,4,7,10))){
                $pre_m[] = date('Ym',$t);
            }
        }
        $_where['dateline IN('.implode(',',$month).') AND 1=?']=1;
        $_where['department_id IN('.implode(',',$this->department_ids).') AND 1=?']=1;
        $datalist = $this->get_month_dao()->find($_where,'dateline ASC',0);
        $data = array();
        foreach($datalist as $val){
            $y = date('Y',strtotime($val['dateline'].'01'));
            $m = date('n',strtotime($val['dateline'].'01'));
            $yearm = $y.'-'.ceil($m/3);//按季度合并

            $data['season'][$yearm]['enter_all_num']   += $val['enter_num'];
            $data['season'][$yearm]['dimiss_all_num']  += $val['active_num']+$val['passive_num'];
            $data['season'][$yearm]['active_all_num']  += $val['active_num'];
            $data['season'][$yearm]['passive_all_num']  += $val['passive_num'];

            if(in_array($val['dateline'],$pre_m)){//季度第一个月
                $data['season'][$yearm]['on_init_num'] += $val['begin_num'];
                $data['season'][$yearm]['on_job_num'] += $val['begin_num']+$data[$yearm]['enter_all_num']-$data[$yearm]['active_all_num']-$data[$yearm]['passive_all_num'];
            }
            //前6个月的数据
            $data['month'][$val['dateline']]['begin_num'] += $val['begin_num'];
            $data['month'][$val['dateline']]['enter_all_num'] += $val['enter_num'];
            $data['month'][$val['dateline']]['dimiss_all_num'] += $val['active_num']+$val['passive_num'];
            $data['month'][$val['dateline']]['active_all_num'] += $val['active_num'];
            $data['month'][$val['dateline']]['passive_all_num'] += $val['passive_num'];
        }

        //计算离职率
        foreach($data['season'] as $ym=>$val){
            $init_num = $val['on_init_num']+$val['enter_all_num'];
            $all_num = $val['on_job_num']+$val['enter_all_num'];
            $data['season'][$ym]['active_rate'] = $this->_count_rate($val['active_all_num'],$init_num);
            $data['season'][$ym]['passive_rate'] = $this->_count_rate($val['passive_all_num'],$init_num);
            $data['season'][$ym]['dimission_rate'] = $this->_count_rate($val['dimiss_all_num'],$all_num);
        }
        //tp($data);
        //计算离职率
        foreach($data['month'] as $ym=>$val){
            $init_num = $val['begin_num']+$val['enter_all_num'];
            $data['month'][$ym]['active_rate'] = $this->_count_rate($val['active_all_num'],$init_num);
            $data['month'][$ym]['passive_rate'] = $this->_count_rate($val['passive_all_num'],$init_num);
            $dimiss_num = $val['active_all_num']+$val['passive_all_num'];
            $data['month'][$ym]['dimission_rate'] = $this->_count_rate($dimiss_num,$init_num);

        }
        return $data;
    }
    /**
     *PM图表
     * @param int $uid
     */
    public function get_pm_by_dep_auth($uid){
        $dep_ids = $this->get_role_dep_ids($uid);
        $_where['department_id IN('.implode(',',$dep_ids).') AND 1=?']=1;
        $end = date('Ymd');
        $_where["enter_date<={$end} AND 1=?"]=1;
        $_where["job_status"]=1;
        $user_list = $this->get_dep_user_dao()->find($_where,'',0);

        $data = array();
        $p_num=$m_num=$max=0;
        if(!empty($user_list)){
            foreach($user_list as $val){
                $str='';
                if($val['p_level']>0){
                    $p = $val['p_level']==99 ? 0 : $val['p_level'];
                    $str = 'P';
                    $p_num +=1;
                }
                if($val['m_level'] > 0){
                    $m = $val['m_level']==99 ? 0 : $val['m_level'];
                    $str = 'M';
                    $m_num +=1;
                }
                if($str){
                    $ec = $str=='P' ? 'P'.$p : 'M'.$m;
                    if(empty($list[$str][$ec]['num'])){
                        $list[$str][$ec]['num'] = 1;
                    }else{
                        $list[$str][$ec]['num'] += 1;
                    }
                }
                $max +=1;
            }
            $data = array();
            //sort
            foreach($list as $k=>$val){
                foreach ($val as $pm=>$value){
                    $value['percent'] = $this->_count_rate($value['num'],$max);
                    $data[$k][$pm] = $value;
                }
            }
            $data['max'] = $max;
            $data['p_num'] = $p_num;
            $data['m_num'] = $m_num;
        }
        return $data;
    }
    /**
     * 离职率
     * @param unknown_type $dimiss
     * @param unknown_type $begin_num
     */
    private function _count_rate($dimiss,$begin_num){
        if(empty($dimiss)){
            return 0;
        }
        return floor(($dimiss/$begin_num)*10000)/10000*100;
    }
    private function _build_pm($p_level,$m_level){
        $is = 1;
        if($p_level > 6 || $m_level > 3){
            $is = 2;
        }
        return $is;
    }
    private function _build_job_pm($p_level,$m_level){
        $str = '';
        if($p_level>0){
            $str = 'p_num';
        }
        if($m_level>0){
            $str = 'm_num';
        }
        return $str;
    }
    /*
     * 得到员工能查看的部门权限
     * @param unknown_type $uid
     * @return multitype:
     */
    public function get_role_dep_ids($uid){
        $allow_dep = array();
        if($uid){
            $dep_role = $this->get_department_by_uid($uid);
            if(!empty($dep_role)){
                foreach($dep_role as $val){
                    $allow_dep[] = $val['department_id'];
                }
            }
        }
        return $allow_dep;
    }

    public function on_job_tpl(){
        return array(
                'fact_num'=>0,
                'plan_num'=>0,
                'p_num'=>0,
                'm_num'=>0,
                'job_time_1'=>0,
                'job_time_2'=>0,
                'job_time_3'=>0,
                'job_time_4'=>0,
                'job_time_5'=>0,
                'degree_1'=>0,
                'degree_2'=>0,
                'degree_3'=>0,
        );
    }
    private function _get_plan_num($function_id,$bu_id){
        $hroa = new Bll_Hr_OutInterface();
        $job_list = $hroa->get_job_list();
        $dep_list = $hroa->get_department_by_business();

        foreach($job_list as $val){
            $fun_id = $dep_list[$val['department_id']]['function_id'];
            $data['function_id'][$fun_id] += $val['plan_num'];
            $b_id = $dep_list[$val['department_id']]['bu_id'];
            if($function_id==$fun_id){
                //tp($val['plan_num']);
                $data['bu_id'][$b_id] += $val['plan_num'];
            }
            if($function_id && $bu_id){
                if($fun_id==$function_id && $bu_id==$b_id){
                    $city_id = $dep_list[$val['department_id']]['city_id'];
                    $data['work_city_id'][$city_id] += $val['plan_num'];
                }
            }
        }
        return $data;
    }
    /**
     * 入职列表
     * @param array() $where
     */
    public function search_on_job_list($where){
        $data = array();
        $end = date('Ymd');
        $key_index = "function_id";
        if(!empty($where['function_id'])){
            $_where['function_id'] = intval($where['function_id']);
            $key_index = "bu_id";
            $f_id = $where['function_id'];
        }else{
            $f_id = 0;
        }
        if(!empty($where['bu_id'])){
            $_where['bu_id'] = intval($where['bu_id']);
            $key_index = "work_city_id";
            $b_id= $where['bu_id'];
        }else{
            $b_id=0;
        }
        $_where["enter_date <={$end} AND 1=?"]=1;
        $_where['job_status'] = 1;
        $datalist = $this->get_dep_user_dao()->find($_where,'',0);

        $tpl = $this->on_job_tpl();
        $data =array(
                999=>$tpl
        );
        $job_ids = array();

        if(!empty($datalist)){
            $job_data = $this->_get_plan_num($f_id,$b_id);

            foreach($datalist as $val){
                if(!$val['function_id']){continue;}//线下有为空的
                if($val['leaving_type']==3){//未来报到
                    //continue;
                }
                $funid =$val[$key_index];
                if(empty($data[$funid])){//init
                    $data[$funid] = $tpl;
                }
                $data[$funid]['function_id']=$val['function_id'];
                $data[$funid]['bu_id']=$val['bu_id'];
                $data[$funid]['work_city_id']=$val['work_city_id'];
                $data[$funid]['department_id']=$val['department_id'];
                if($key_index=='bu_id'){
                    $data[$funid]['city_list'][$val['work_city_id']] = 1;
                }
                $data[$funid]['fact_num'] +=1;
                $data[999]['fact_num'] +=1;
                if(empty($data[$funid]['plan_num'])){
                    $data[$funid]['plan_num'] =$job_data[$key_index][$funid];
                    $data[999]['plan_num'] +=$job_data[$key_index][$funid];
                }
                if($val['p_level'] > 0){
                    $data[$funid]['p_num'] +=1;
                    $data[999]['p_num'] +=1;
                }
                if($val['m_level'] > 0){
                    $data[$funid]['m_num'] +=1;
                    $data[999]['m_num'] +=1;
                }

                $time = $this->build_enter_job_time($val['enter_date']);
                $st[$time][] = $val['enter_date'];
                $data[$funid]['job_time_'.$time] +=1;
                $data[999]['job_time_'.$time] +=1;
                $degree = 3;
                if($val['degree']==4||$val['degree']==5){
                    $degree = 1;
                }elseif($val['degree']==1||$val['degree']==2){
                    $degree = 3;
                }elseif($val['degree']==3){
                    $degree = 2;
                }
                if($degree){
                    $data[$funid]['degree_'.$degree] +=1;
                    $data[999]['degree_'.$degree] +=1;
                }
            }
        }
        return $data;
    }
    /**
     * 离职列表
     * @param array $where
     */
    public function search_dimission_list($where){
        $data = array();
        if(empty($where['end'])){
            return $data;
        }
        $key_index = "function_id";
        if(!empty($where['function_id'])){
            $_where['function_id'] = intval($where['function_id']);
            $key_index = "bu_id";
        }
        if(!empty($where['bu_id'])){
            $_where['bu_id'] = intval($where['bu_id']);
            $key_index = "work_city_id";
        }
        //$_where["last_date >={$where['start']} AND last_date <={$where['end']} AND 1=?"]=1;
        $datalist = $this->get_dep_user_dao()->find($_where,'',0);
        $start = $where['start'];
        $end = $where['end'];
        $data = $this->_build_for_all_list($datalist,$start,$end,$key_index);
        return $data;
    }
    /**
     * 离职数据列表
     * @return multitype:number
     */
    public function build_data_tpl(){
        $tpl = array(
                'enter_num'=>0,
                'dimission_num'=>0,
                'on_job_num'=>0,
                'leave_type_1'=>0,//主动离职
                'leave_type_2'=>0,//被动离职
                'pm_1'=>0,
                'pm_2'=>0,
                'job_time_1'=>0,
                'job_time_2'=>0,
                'job_time_3'=>0,
                'job_time_4'=>0,
                'job_time_5'=>0,
                'job_time_6'=>0,
        );
        return $tpl;
    }
    /**
     *
     * @param array $datalist
     * @param int $start
     * @param int $end
     * @param string $type function_id|bu_id|city_id
     * @return Ambigous <number, multitype:unknown multitype:number  >
     */
    private function _build_for_all_list($datalist,$start,$end,$type){
        $tpl = $this->build_data_tpl();
        $data =array(
                999=>$tpl
        );
        if(!empty($datalist)){
            foreach($datalist as $val){
                if(!$val['function_id']){continue;}//线下有为空的
                if($val['leaving_type']==3){//未来报到
                    continue;
                }
                $funid =$val[$type];
                if(empty($data[$funid])){//init
                    $data[$funid] = $tpl;
                }
                $data[$funid]['function_id']=$val['function_id'];
                $data[$funid]['bu_id']=$val['bu_id'];
                $data[$funid]['work_city_id']=$val['work_city_id'];
                $data[$funid]['department_id']=$val['department_id'];
                //index
                $list_index = $val['function_id'].'-'.$val['bu_id'].'-'.$val['work_city_id'];
                $data[$funid][$list_index] = $val['department_id'];
                if($type=='bu_id'){
                    $data[$funid]['city_list'][$val['work_city_id']] = 1;
                }

                if($val['enter_date'] >= $start && $val['enter_date'] <= $end){//入职人数
                    $data[$funid]['enter_num'] +=1;
                    $data[999]['enter_num'] +=1;
                }
                if($val['enter_date'] < $start && (empty($val['last_date']) || $val['last_date'] >= $start)){//期初人数
                    $data[$funid]['on_job_num'] +=1;
                    $data[999]['on_job_num'] +=1;
                }
                //试用和正式才算离职,算在职人数
                if(!in_array($val['user_type'],array(1,2))){
                    continue;
                }
                if($val['last_date'] >= $start && $val['last_date'] <= $end && $val['leaving_type']){//离职人数
                    $data[$funid]['dimission_num'] +=1;
                    $data[$funid]['dep_ids'][] = $val['department_id'];
                    //pm
                    $pm = $this->_build_pm($val['p_level'],$val['m_level']);
                    $data[$funid]['pm_'.$pm] +=1;
                    //在职时长
                    $time = $this->build_dimission_job_time($val['enter_date'],$val['last_date']);
                    $data[$funid]['job_time_'.$time] +=1;
                    $data[$funid]['leave_type_'.$val['leaving_type']] +=1;

                    $data[999]['dimission_num'] +=1;
                    $data[999]['pm_'.$pm] +=1;
                    $data[999]['job_time_'.$time] +=1;
                    $data[999]['leave_type_'.$val['leaving_type']] +=1;
                }
            }
        }
        //tp($data);
        return $data;
    }

    /**
     * 3个月以内、[4-6)个月、[0.5-1）年、[1-1.5)年、[1.5-2)年、[2年以上
     * 按月份
     * @param int $enter_date 20130101
     */
    public function build_dimission_job_time($enter_date,$last_date){
        $today = date('Ymd');
        $enter_t = strtotime($enter_date);
        $last_t = 0;
        if($last_date){
            $last_t = $last_date;
        }
        $list = array(
                1=>array(0,3),
                2=>array(3,6),
                3=>array(6,12),
                4=>array(12,18),
                5=>array(18,24),
                6=>array(24,10000000),
        );
        $type=1;
        foreach ($list as $k=>$val){
            if(!$last_t){
                continue;
            }
            $cktime=$this->_check_last_time($enter_t,$val);

            $p_start_unix = $cktime[0];
            $p_end_unix = $cktime[1];
            $p_start = date('Ymd',$p_start_unix);
            $p_end = date('Ymd',$p_end_unix);
            if($last_t >= $p_start && $last_t < $p_end){
                $type = $k;break;
            }
        }
        return $type;
    }
    /**
     * 半年以内、0.5-1、1-2年、2-3年、3年以上
     * 按月份
     * @param int $enter_date 20130101
     */
    public function build_enter_job_time($enter_date){
        $today = date('Ymd');
        $enter_t = strtotime($enter_date);
        $list = array(
                1=>array(0,6),
                2=>array(6,12),
                3=>array(12,24),
                4=>array(24,36),
                5=>array(36,10000000),
                );
        $type=1;
        foreach ($list as $k=>$val){
            $cktime=$this->_check_last_time($enter_t,$val);
            $p_start_unix = $cktime[0];
            $p_end_unix = $cktime[1];
            $p_start = date('Ymd',$p_start_unix);
            $p_end = date('Ymd',$p_end_unix);
            if($today >= $p_start && $today < $p_end){
                $type = $k;break;
            }
        }
        return $type;
    }
    /**
     * 区分开始的月份是2月，结束月份是2月时需要特殊处理
     * @param int $enter_t
     * @param int $start
     * @param int $end
     */
    private function _check_last_time($enter_t,$val){
        $d = date('d',$enter_t);
        $p_start_unix =  mktime(0,0,0,date('m',$enter_t)+$val[0],1,date('Y',$enter_t));
        $p_end_unix = mktime(0,0,0,date('m',$enter_t)+$val[1],1,date('Y',$enter_t));
        $e_m = date('m',$p_end_unix);
        $s_m = date('m',$p_start_unix);

        if($e_m==2){//结束月份是2月
            $end_day = date('t',$p_end_unix);//取这个月的最后一天
            if($end_day >28 && $d>$end_day){
                $m = date('m',$enter_t)+$val[1]+1;//3月
                $p_end_unix = mktime(0,0,0,$m,0,date('Y',$enter_t));
            }else{
                $p_end_unix = mktime(0,0,0,date('m',$enter_t)+$val[1],$end_day,date('Y',$enter_t));
            }
        }else{
            $p_end_unix = mktime(0,0,0,date('m',$enter_t)+$val[1],$d,date('Y',$enter_t));
        }
        if($s_m==2){//开始的月份是2月
            $s_end_day = date('t',$p_start_unix);//取这个月的最后一天
            if($s_end_day >28 && $d>$s_end_day){
                $p_start_unix = mktime(0,0,0,date('m',$enter_t)+$val[0]+1,0,date('Y',$enter_t));
            }else{
                $p_start_unix = mktime(0,0,0,date('m',$enter_t)+$val[0],$s_end_day,date('Y',$enter_t));
            }
        }else{
            $p_start_unix = mktime(0,0,0,date('m',$enter_t)+$val[0],$d,date('Y',$enter_t));
        }
        return array($p_start_unix,$p_end_unix);
    }
    private $_user_role = array();
    /**
     * 获取员工能查看的部门
     * @param int $uid
     * @return multitype:
     */
    public function get_department_by_uid($uid){
        if(isset($this->_user_role[$uid])){
            return $this->_user_role[$uid];
        }
        $data = array();
        if(empty($uid)){
            return $data;
        }
        $_where = array(
                'user_id'=>intval($uid)
        );
        $_tmp_deplist = $this->get_user_role()->find($_where,'',0);
        if(!empty($_tmp_deplist)){
            $this->_user_role[$uid]=$_tmp_deplist;
        }
        return $this->_user_role[$uid];
    }
    /**
     * 批量写入权限
     */
    public function multi_insert($data){
        $dep_ids = array_keys($data);
        //
        $_where['user_id'] = $data[$dep_ids[0]]['user_id'];
        $_where['department_id IN('.implode(',',$dep_ids).') AND 1=?']=1;

        $this->get_user_role()->remove($_where);

            $this->get_user_role()->batch_insert($data);

        return true;
    }
    /**
     * 离职人员 数据源dep_user
     * @param array $cityid
     * @param int $start
     * @param int $end
     * @return multitype:
     */
    public function get_user_by_dep_ids($depid,$start,$end){
        $data = array();
        if(empty($depid) || empty($start) || empty($end)){
            return $data;
        }
        if(is_array($depid)){
            $_where["department_id IN(".implode(',',$depid).") AND 1=?"]=1;
        }else{
            $_where["department_id"]=$depid;
        }

        $_where["last_date>={$start} AND last_date<={$end} AND 1=?"]=1;
        $data= $this->get_dep_user_dao()->find_assoc($_where,'',0);
        return $data;
    }
    /**
     * 取员工基本信息
     * @param unknown_type $uids
     */
    public function get_user_by_ids($uids){
        $_where['user_id IN('.implode(',',$uids).') AND 1=?']=1;
        return $this->get_dep_user_dao()->find_assoc($_where,'',0);
    }
    /**
     * 返回所有在职用户
     */
    public function get_all_job_user(){
        return $this->get_dep_user_dao()->find(array('job_status'=>1),'',0);
    }
    /**
     * 删除 权限
     * @param unknown_type $ids
     */
    public function delete_role($ids){
        return $this->get_user_role()->remove_by_ids($ids);
    }
    //report_user_role
    private function get_user_role(){
        $dao = $this->get_dao('Dao_Report_Role');
        return $dao;
    }
    //视图表
    private function get_dep_user_dao (){
        $dao = $this->get_dao('Dao_Report_DepUser');
        return $dao;
    }
    private function get_month_dao(){
        $dao = $this->get_dao('Dao_Report_ReportMonth');
        return $dao;
    }
}
